﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace StockMatch.Host
{
    public class Wcf
    {
        public static T CreateClientProxy<T>(string serverAddress, int port, string path, int maxConnections = 100)
        {
            //创建服务通道 获取服务代理
            NetTcpBinding bind = new NetTcpBinding();
            bind.Security.Mode = SecurityMode.None;
            bind.MaxConnections = maxConnections;
            bind.ReceiveTimeout = new TimeSpan(10, 10, 10);
            bind.SendTimeout = new TimeSpan(10, 10, 10);
            bind.MaxReceivedMessageSize = int.MaxValue;
            bind.MaxBufferSize = int.MaxValue;
            bind.ReaderQuotas.MaxArrayLength = int.MaxValue;
            bind.ReaderQuotas.MaxStringContentLength = int.MaxValue;


            //ChannelFactory<ILogMonitor> factory = new ChannelFactory<ILogMonitor>(bind, new EndpointAddress(string.Format("net.tcp://{0}:2012/ILogMonitor", serverAddress)));
            ChannelFactory<T> factory = new ChannelFactory<T>(bind, new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}", serverAddress, port, path)));
            //ChannelFactory<T> factory = new ChannelFactory<T>(bind, new EndpointAddress(uri));
            foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
            {
                DataContractSerializerOperationBehavior dataContractBehavior =
                            op.Behaviors.Find<DataContractSerializerOperationBehavior>()
                            as DataContractSerializerOperationBehavior;
                if (dataContractBehavior != null)
                {
                    dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
                }
            }


            T proxy = factory.CreateChannel();
            //IMessageServer proxy = ChannelFactory<IMessageServer>.CreateChannel(bind, new EndpointAddress(u2.ToString()));
            return proxy;
        }

        public static ServiceHost CreateServiceHost<I, T>(int port, string path, int maxConnections = 100)
        {
            //基础数据服务
            ServiceHost _baseServiceHostBaseManager = new ServiceHost(typeof(T));


            NetTcpBinding _baseBindingBaseManager = new NetTcpBinding();
            _baseBindingBaseManager.MaxConnections = maxConnections;
            _baseBindingBaseManager.ReceiveTimeout = new TimeSpan(10, 10, 10);
            _baseBindingBaseManager.SendTimeout = new TimeSpan(10, 10, 10);
            _baseBindingBaseManager.Security.Mode = SecurityMode.None;
            _baseBindingBaseManager.MaxReceivedMessageSize = int.MaxValue;
            _baseBindingBaseManager.MaxBufferSize = int.MaxValue;
            _baseBindingBaseManager.ReaderQuotas.MaxArrayLength = int.MaxValue;
            _baseServiceHostBaseManager.AddServiceEndpoint(typeof(I), _baseBindingBaseManager, new EndpointAddress(string.Format("net.tcp://localhost:{0}/{1}", port, path)).ToString());
            ServiceThrottlingBehavior _stbBaseManager = new ServiceThrottlingBehavior();
            _stbBaseManager.MaxConcurrentCalls = int.MaxValue;
            _stbBaseManager.MaxConcurrentInstances = int.MaxValue;
            _stbBaseManager.MaxConcurrentSessions = int.MaxValue;

            _baseServiceHostBaseManager.Description.Behaviors.Add(_stbBaseManager);
            //_baseServiceHostBaseManager.Open();


            return _baseServiceHostBaseManager;


        }

        public static ServiceHost OpenServiceHost<I, T>(int port, string path, int maxConnections = 100)
        {
            //基础数据服务
            ServiceHost _baseServiceHostBaseManager = new ServiceHost(typeof(T));


            NetTcpBinding _baseBindingBaseManager = new NetTcpBinding();
            _baseBindingBaseManager.MaxConnections = maxConnections;
            _baseBindingBaseManager.Security.Mode = SecurityMode.None;
            _baseBindingBaseManager.MaxReceivedMessageSize = int.MaxValue;
            _baseBindingBaseManager.MaxBufferSize = int.MaxValue;
            _baseBindingBaseManager.ReaderQuotas.MaxArrayLength = int.MaxValue;
            _baseServiceHostBaseManager.AddServiceEndpoint(typeof(I), _baseBindingBaseManager, new EndpointAddress(string.Format("net.tcp://localhost:{0}/{1}", port, path)).ToString());
            ServiceThrottlingBehavior _stbBaseManager = new ServiceThrottlingBehavior();
            _stbBaseManager.MaxConcurrentCalls = int.MaxValue;
            _stbBaseManager.MaxConcurrentInstances = int.MaxValue;
            _stbBaseManager.MaxConcurrentSessions = int.MaxValue;

            _baseServiceHostBaseManager.Description.Behaviors.Add(_stbBaseManager);
            _baseServiceHostBaseManager.Open();

            return _baseServiceHostBaseManager;



        }


        public static void Close(object obj)
        {
            ICommunicationObject c = (ICommunicationObject)obj;
            try
            {
                c.Close();
            } 
            catch
            { 
                c.Abort();
                throw;
            }   
        }
    }
}
